<template>
	<view class="main">
		<view class="db_title f30 bot_line">
			新增待办
		</view>
		<view class="form">
			<view class="db_row bot_line">
				<view class="icon">
					<image src="../../static/image/sxmc.png" mode="widthFix"></image>
				</view>
				<view class="con gui-flex gui-space-between">
					<view class="gui-form-body gui-flex gui-align-items-center">
						<input type="text" class="gui-form-input f24" v-model="formData.title" name="title"
							placeholder="请输入事项名称" />
					</view>
					<text class="gui-form-icon gui-icons gui-text-center gui-color-gray"
						v-if="formData.title.length >= 1" @click="clearName2">&#xe78a;</text>
					<view class="label f24">
						事项名称
					</view>
				</view>
			</view>
			<view class="db_row bot_line">
				<view class="icon">
					<image src="../../static/image/sxrq.png" mode="widthFix"></image>
				</view>
				<view class="con gui-flex gui-space-between">
					<view class="input_warp">
						<picker mode="time" :value="formData.date" start="00:00" end="24:00" @change="bindTimeChange">
							<view class="select_stype" v-if="!formData.date">
								<text class="gui-icons c999 f22">
									&#xe6c7;
								</text>
								<text class="c999 f24 txt">选择时间</text>
							</view>
							<view class="val f24" v-else>
								{{formData.date}}
							</view>
						</picker>
					</view>
					<view class="label f24">
						事项时间
					</view>
				</view>
			</view>
			<view class="db_row bot_line">
				<view class="icon">
					<image src="../../static/image/xgkh.png" mode="widthFix"></image>
				</view>
				<view class="con gui-flex gui-space-between">
					<view class="input_warp">
						<view class="select_stype" @tap="$u.to(`/pages/clientSelect/clientSelect?type=select`)">
							<text class="gui-icons c999 f22">
								&#xe6c7;
							</text>
							<text class="c999 f24 txt">选择客户</text>
						</view>
					</view>
					<view class="label f24">
						客户相关（选填）
					</view>
				</view>
			</view>
			<view class="db_row bot_line" v-if="clientList.length > 0">
				<view class="icon">
					<!-- <image src="../../static/image/xgkh.png" mode="widthFix"></image> -->
				</view>
				<view class="con gui-flex gui-wrap">
					<view class="selectWarp f24" @click="delClient(index)" v-for="(item,index) in clientList"
						:key="index">
						{{item.name}}
						<text class="gui-icons" style="margin-left: 10rpx;">&#xe7a5;</text>
					</view>
				</view>
			</view>

			<view class="db_row bot_line" style="align-items: flex-start;">
				<view class="icon">
					<image src="../../static/image/sx.png" mode="widthFix"></image>
				</view>
				<view class="con">
					<view class="sx_item mt_20" v-for="(item,index) in formData.itemList" :key="index">
						<input type="text" class="f24" v-model="item.value" placeholder="输入事项" />
						<text class="gui-icons f24" @click="delItem(index)" v-if="index > 0">&#xe7a5;</text>
					</view>
					<!-- <view class="sx_item">
						<input type="text" class="f24" placeholder="输入事项" />
					</view> -->
					<view class="add_item" @click="addSx">
						+添加事项
					</view>
				</view>
			</view>
			<view class="db_row bot_line" style="align-items: flex-start;">
				<view class="icon">
					<image src="../../static/image/bz.png" mode="widthFix"></image>
				</view>
				<view class="con">
					<textarea class="textarea_warp f24" v-model="formData.remark" placeholder="备注(选填)"></textarea>
				</view>
			</view>
		</view>
		<view class="btn_warp">
			<button class="btn_log" @tap="addBacklog">保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				today: '',
				isClick: true,
				formData: {
					title: '',
					itemList: [{
						value: ''
					}],
					date: '',
					remark: ''
				},
				date: '',
				clientList: []
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		onLoad(e) {
			if(e.time) {
				this.today = e.time;
			}
			uni.$on('selectChange', (item) => {
				let isBao = false;
				this.clientList.some(e => {
					if (e.id == item.id) {
						isBao = true;
						return true;
					}
				})
				if (!isBao) {
					this.clientList.push(item)
				}
			})
		},
		onUnload() {
			uni.$off('selectChange')
		},
		methods: {
			addSx() {
				this.formData.itemList.push({
					value: ''
				})
			},
			// 删除客户
			delClient(index) {
				this.clientList.splice(index, 1);
			},
			delItem(index) {
				this.formData.itemList.splice(index, 1)
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
			},
			bindTimeChange: function(e) {
				this.formData.date = e.detail.value
			},
			// 清空输入框的值
			clearName2: function() {
				console.log(11);
				this.formData.title = '';
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			// 新增待办
			addBacklog() {
				if(!this.formData.title) return this.$u.msg(`请输入事项名称`);
				if(!this.formData.date) return this.$u.msg(`请选择事项时间`);
				if(this.formData.itemList.length <= 0) return this.$u.msg(`请至少添加一个事项`);
				let formParam = {
					title: '',
					date: '',
					event: '',
					clientId: '',
					clientName: '',
					remark: ''
				}
				formParam.title = this.formData.title;
				formParam.date = this.today + ' ' + this.formData.date;
				if (this.formData.itemList.length > 0) {
					// let event = this.formData.itemList.map(item => item.value);
					formParam.event = JSON.stringify(this.formData.itemList);
				}
				if (this.clientList.length > 0) {
					let idList = [];
					let nameList = [];
					this.clientList.forEach(e => {
						idList.push(e.id);
						nameList.push(e.name);
					})
					formParam.clientId = idList.join(',');
					formParam.clientName = nameList.join(',');
				}
				formParam.remark = this.formData.remark;
				if(this.isClick) {
					this.isClick = !this.isClick;
				}else {
					return;
				}
				this.$api.addBacklog(formParam).then(res => {
					this.$u.msg("添加成功");
					uni.$emit('updateBacklog')
					setTimeout(() => {
						this.$u.back();
					}, 1000)
				}).finally(e => {
					this.isClick = true;
				})
			}
		}
	}
</script>

<style lang="scss">
	.main {
		padding: 36rpx;
	}

	.btn_warp {
		margin-top: 80rpx;
	}

	.btn_log {
		width: 600rpx;
		height: 86rpx;
		background: linear-gradient(180deg, #7EC1FF 0%, #0A60FF 100%);
		border-radius: 50rpx 50rpx 50rpx 50rpx;
		opacity: 1;
		font-size: 28rpx;
		color: rgba(255, 255, 255, 0.85);
		line-height: 86rpx;
	}

	.selectWarp {
		padding: 0 30rpx;
		width: auto;
		height: 60rpx;
		background: rgba(14, 110, 255, 0.15);
		border-radius: 30rpx;
		border: 1rpx solid rgba(165, 128, 232, 0.2);
		display: flex;
		align-items: center;
		justify-content: center;
		color: #0E6EFF;
		margin-right: 20rpx;
		margin-bottom: 10rpx;
	}
</style>